﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media;

namespace GenTheme.Parsing
{
    // Just for testing
    internal struct StepInfo
    {
        private int x;
        private int y;
        private int stepValue;

        public StepInfo(Color[,] oArr, int fromX, int fromY)
        {
            this.x = -1;
            this.y = -1;
            this.stepValue = -1;

            int curX = fromX;
            int curY = fromY;

            while (this.x == -1 && this.y == -1 && curY < oArr.GetLength(0))
            {
                curX = 0;
                while (curX < oArr.GetLength(1))
                {
                    if (this.IsRootPixel(oArr, curX, curY))
                    {
                        if (this.x == -1 && this.y == -1)
                        {
                            // Fill start position after finding first root pixel
                            this.x = curX;
                            this.y = curY;
                        }
                        else
                        {
                            // Fill distance between root pixels after finding the second in a row and quit
                            if (this.y == curY)
                            {
                                this.stepValue = curX - this.x;
                                return;
                            }
                            else
                            {
                                System.Windows.MessageBox.Show("StrangeBehavor this.x <> curX");
                            }
                        }
                    }
                    curX++;
                }
                curY++;
            }
        }

        /// <summary>
        /// Checking for root pixel
        /// </summary>
        private bool IsRootPixel(Color[,] oArr, int x, int y)
        {
            // 1 0 1
            // 0 1 0
            // 1 0 1
            if (x != 0 && y != 0 && x < oArr.GetLength(1) - 1 && y < oArr.GetLength(0) - 1
                && oArr[y - 1, x - 1] == oArr[y, x] && oArr[y + 1, x - 1] == oArr[y, x] && oArr[y - 1, x + 1] == oArr[y, x] && oArr[y + 1, x + 1] == oArr[y, x]
                && oArr[y - 1, x] != oArr[y, x] && oArr[y + 1, x] != oArr[y, x] && oArr[y, x + 1] != oArr[y, x] && oArr[y, x - 1] != oArr[y, x])
            {
                return true;
            }

            return false;
        }

        #region public

        public int Y
        {
            get
            {
                return this.y;
            }
        }

        public int X
        {
            get
            {
                return this.x;
            }
        }

        public int Value
        {
            get
            {
                return this.stepValue;
            }
        }

        #endregion

    }
}
